import React, { Component } from 'react';
import {Layout, Button, Tooltip ,Select,Input,Row, Col,Table, Switch, Space } from 'antd';
import '../App.css';
import { SearchOutlined} from '@ant-design/icons';
import Highlighter from 'react-highlight-words';

import '../pages/webExcel.css'
import {getDimMbrs} from "../api/dim";
const { Header, Content, Footer } = Layout;
const { Option } = Select;

export default class DimTree extends Component {


    constructor(props) {
        super(props)
        this.state = {
            dimType:this.props.selectDimType,
            dimMbrData:[],
            tableKey:-1,
            expandAllRowsFlag:false,
            searchText: '',
            searchedColumn: '',
        }
    }

    componentWillReceiveProps(nextProps){

        let _dimType=nextProps.selectDimType+0;
        //alert(_dimType)

        //查询后端  把后端数据组织到前端
        getDimMbrs(_dimType).then(
            (res) => {
                // let keyVal=this.state.tableKey;
                // keyVal++;
                // console.log("keyVal:"+keyVal)
                this.setState({dimMbrData: res.data.children});
            },
            (error) => {
                console.log("get response failed!");
            }
        );

    }
//通过改变table tree  控件的 key  达到目的
    expandTree=()=>{
        let tableKey=this.state.tableKey;
        tableKey++;
        this.setState({tableKey:tableKey,expandAllRowsFlag:true})
    }
    //收缩
    collapseTree=()=>{
        let tableKey=this.state.tableKey;
        tableKey++;
        this.setState({tableKey:tableKey,expandAllRowsFlag:false})
    }


    componentDidMount() {
        this.props.onRef(this)  //这个就是把自身传给调用者
    }


    render() {

        const columns = [
            {
                title: '名称',
                dataIndex: 'text',
                key: 'key',
            },
        ];


        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                //console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
            },
            onSelect: (record, selected, selectedRows) => {
                //因为删除了一项啊 所以这里要减去！
                if(selectedRows[0]==undefined){
                    selectedRows.splice(0,1)
                }
                this.props.selectRowsMethod(selectedRows);
             },
            onSelectAll: (selected, selectedRows, changeRows) => {
              //  console.log(selected, selectedRows, changeRows);
            },
        };

        return (
            <>
                {/*<Space align="center" style={{ marginBottom: 16 }}>*/}
                    {/*CheckStrictly: <Switch checked={checkStrictly} onChange={setCheckStrictly} />*/}
                {/*</Space>*/}
             <Table
                   key={this.state.tableKey}
                    columns={columns}
                    rowSelection={{ ...rowSelection,checkStrictly:true}}
                    dataSource={this.state.dimMbrData}
                   // defaultExpandAll = {true}
                   defaultExpandAllRows={this.state.expandAllRowsFlag}
             />


            </>
        )
    }



}
